package algorithm.graph;

import java.util.Vector;

/**
 * 稠密图：邻接矩阵
 * @author bingo
 * @Date 2018/3/11
 */
public class DenseGraph implements Graph {

    /**顶点数*/
    private int n;
    /**边数*/
    private int m;
    /**是否为有向图*/
    private boolean directed;

    /**图的具体数据*/
    private boolean[][] g;
    /**初始化没有任何边，定点之间的连接为false*/
    public DenseGraph(int n, boolean directed) {
        this.n = n;
        this.directed = directed;
        this.m = 0;
        g = new boolean[n][n];
    }
      /**为图增加一条边**/
    public void addEdge(int v,int w){
        if(hasEdge(v,w)){
            return;
        }

        g[v][w] = true;
        if(!directed){
            g[w][v] = true;
        }
        m++;


    }
    /**判断两个顶点之间有没有边**/
    public boolean hasEdge(int v,int w){

        assert v >= 0 && v < n;
        assert w >= 0 && w < n;

        return g[v][w];

    }
    /**返回图中一个顶点的所有邻边*/
    public Iterable<Integer> adj(int v){

        assert v >=0 && v < n;

        Vector<Integer> vector = new Vector();

        for(int i=0;i<n;i++){
            if(g[v][i]){
                vector.add(i);
            }
        }
        return   vector;
    }

    @Override
    public void show() {

        for(int i=0;i<n;i++){

            for (int j=0;j<n;j++){

                System.out.print(g[i][j]+"\t");
            }
            System.out.println();
        }
    }

    @Override
    public int n() {
        return n;
    }

    @Override
    public int m() {
        return m;
    }



}
